package pdfsetter.core.schema.onefile;

import java.util.Iterator;

import pdfsetter.core.schema.FillingSchema;
import pdfsetter.core.schema.PageSelection;
import pdfsetter.core.schema.advanced.FilePageSelection;
import pdfsetter.core.util.RandomAccessIterable;
import pdfsetter.core.util.RandomAccessIterableIterator;

/**
 * This class represents {@link FillingSchema} in which we take all pages from only one file. We simply need {@link FilePageSelection} and file number to
 * produce such a schema.
 * 
 * @author Andrzej Dorobisz
 */
public class FixedFileFillingSchema implements FillingSchema {
    private final int fileNumber;
    private final FilePageSelection filePageSelection;

    public FixedFileFillingSchema(int fileNumber, FilePageSelection filePageSelection) {
        this.fileNumber = fileNumber;
        this.filePageSelection = filePageSelection;
    }

    @Override
    public RandomAccessIterable<PageSelection> getPageSelections() {
        return new RandomAccessIterable<PageSelection>() {

            @Override
            public Iterator<PageSelection> iterator() {
                return new RandomAccessIterableIterator<PageSelection>(this);
            }

            @Override
            public PageSelection get(int index) {
                return new PageSelection(fileNumber, filePageSelection.get(index));
            }

            @Override
            public int size() {
                return filePageSelection.size();
            }
        };
    }

    @Override
    public int getExpectedNumberOfFiles() {
        return fileNumber;
    }
}
